import collections


class Solution(object):
    def mostCommonWord(self, paragraph, banned):
        """
        :type paragraph: str
        :type banned: List[str]
        :rtype: str
        """
        banned = set(banned)
        paragraph = paragraph.lower()
        p = []
        left = 0
        right = 0
        n = len(paragraph)
        while right < n:
            if not 'a' <= paragraph[right] <= 'z':
                if right > left:
                    p.append(paragraph[left: right])
                left = right + 1
            right += 1
        if right > left:
            p.append(paragraph[left: right])
        c = collections.Counter(p)
        d = [[-c[key], key] for key in c]
        d.sort()
        for w in d:
            if w[1] not in banned:
                return w[1]


data = Solution()
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
print(data.mostCommonWord(paragraph, banned))
paragraph = "a, a, a, a, b,b,b,c, c"
banned = ["a"]
print(data.mostCommonWord(paragraph, banned))